"""
@Project: Rebar
@File: custom_response.py
@Author: hzc
@Date: 2023/3/21 20:23
"""
from enum import Enum
from starlette import status
from fastapi.responses import JSONResponse
from typing import Union


# --------------------------- 自定义响应 start  --------------------------------#
def template_response(data: Union[list, dict, str, None], code: Union[Enum, dict]) -> JSONResponse:
    """
    返回统一的响应模板
    :param data: 响应数据
    :param code: 响应码和响应信息
    :return: JSONResponse
    """
    if isinstance(code, Enum):
        code = code.value
    return JSONResponse(
        headers={'content-type': 'application/json',
                 'access-control-allow-origin': '*',
                 'access-control-allow-credentials': 'true'
                 },
        status_code=status.HTTP_200_OK,
        content={
            'code': code["code"],
            'message': code["message"],
            'data': data
        }
    )


def success_res(data, code: Union[Enum, dict]) -> JSONResponse:
    """
    成功响应
    :param data:
    :param code:
    :return:
    """
    return template_response(data, code)


def error_res(code: Union[Enum, dict], data=None) -> JSONResponse:
    """
    错误响应
    :param data: data默认为None
    :param code:
    :return:
    """
    return template_response(data, code)


# --------------------------- 自定义响应 end  --------------------------------#


# --------------------------- 自定义响应码 start  --------------------------------#
def code_temp(code: int, message: str) -> dict:
    """
    生成响应信息的模板
    :param code:
    :param message:
    :return: dict
    """
    return {
        'code': code,
        'message': message
    }


class SuccessCode:
    """
    成功响应信息
    """

    LOGIN = code_temp(200200, "用户登录成功")
    REGISTER = code_temp(200200, "用户注册成功")
    USER_LOGGED_IN = code_temp(200200, "用户已登录，请勿重复登录")
    USER_LOGOUT = code_temp(200200, "用户已退出登录")
    POST = code_temp(200200, "数据添加成功")

    PUT = code_temp(200200, "数据编辑成功")
    PUT_SAME_DATE = code_temp(200200, "数据相同，无需修改")
    GET = code_temp(200200, "数据获取成功")
    DELETE = code_temp(200200, "数据删除成功")
    ACTIVATE = code_temp(200200, "用户激活成功")
    USER_HAS_ACTIVATED = code_temp(200200, "用户已激活")
    DATA_ZERO = code_temp(200200, "无数据")
    EMAILCODE_SEND = code_temp(200200, "验证码发送成功")
    EMAILCODE_OK = code_temp(200200, "验证成功")
    UPDATE_SUCC =code_temp(200200, "密码修改成功")

class ErrorCode:
    """
    服务器错误响应 500500 - 500510
    """
    SERVER = code_temp(500500, "服务器异常")
    """
    数据库操作异常  500560-500570
    """
    USER_ISEXIST = code_temp(500560, "用户已存在")
    EMAIL_ISEXIST = code_temp(500561, "邮箱已被占用")
    EMAIL_NOT_EXIST = code_temp(500561, "邮箱不存在")
    """
        Email_code相关的错误响应 500550-500559
    """
    EMAIL_CODE_ISNONE =code_temp(500550, "验证码已过期")
    EMAIL_CODE_ERROR = code_temp(500551, "验证码输入错误")
    EMAIL_CODE_NOTOK = code_temp(500553, "发送验证码失败")

    """
            登录图片验证码相关的错误响应
    """
    LOGIN_CODE_ERROR =  code_temp(500554, "验证码错误")
    LOGIN_CODE_NOTOK = code_temp(500555, "验证码过期")
    """
    token相关的错误响应 500511-500520
    """
    MISSING_TOKEN = code_temp(500511, "请携带token")
    INVALID_TOKEN = code_temp(500512, "请携带合法token")
    EXPIRED_TOKEN = code_temp(500513, "登录超时，请重新登录")
    FAILED_GENERATE_TOKEN = code_temp(500514, "生成token失败")

    """
    用户相关错误响应信息 500521 - 500530
    """
    USER_PWD_ERROR = code_temp(500521, "用户名或密码错误")
    OLD_PWD_ERROR = code_temp(500521, "旧密码错误")
    USER_NOT_EXISTED = code_temp(500522, "用户名不存在")
    USER_NOT_ACTIVATED = code_temp(500523, "用户未激活")
    MAX_ONLINE_USERS = code_temp(500524, "当前在线人数已达到最大")
    DATA_NOT_EXISTED = code_temp(500525, "{}不存在")
    USER_NO_PERMISSION = code_temp(500526, "权限不足")
    USER_MUST_SELF = code_temp(500527, "操作需用户本人进行")
    """
    数据操作错误响应信息 500531 - 500540
    """
    POST = code_temp(500531, "数据添加失败")
    PATCH = code_temp(500532, "数据编辑失败")
    GET = code_temp(500533, "数据获取失败")
    DELETE = code_temp(500534, "数据删除失败")
    DELETE_USER_LOGGED_IN = code_temp(500535, "无法删除已登录的用户，请先退出登录")
    """
    数据库相关错误响应信息 500541 - 500550
    """
    UNIQUE_ERROR = code_temp(500541, "数据已存在，请勿重复添加")
    SQL_ERROR = code_temp(500542, "数据库错误")
    SQL_TIMEOUT = code_temp(500543, "数据库连接已断开，请重试")
    # {"code": 500541, "message": f"{}已存在，请勿重复添加"}
    """
        模型训练相关错误响应信息
    """
    SAMPLENUM_ERROR = code_temp(500551, "样本数据太少，无法拟合")
    DATA_ERROR = code_temp(500551, "输入数据有误，请检查！")
    Param_ERROR = code_temp(500551, "输入参数不合法")
    CANT_ERROR = code_temp(500553, "未找到最优参数、请重新设置")
    INPUTNUM_ERROR = code_temp(500552, "输入参数个数只能在2-10个之间")
# ------------------------ 自定义响应 end  ----------------------------#
